﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Roton.ZZT
{
    partial class Context
    {
        protected IAudio audio;
        protected IDisplay display;
        protected IInput input;
        protected IMemory memory;
        protected Parameters parameters;
        protected Random random;
        protected int randomSeed;
        protected IVideo video;

        public bool InterruptTimer;
        public int InterruptTotal;

        public Context(IMemory memory, IVideo video, IAudio audio, IInput input, EventTimer timer, Parameters parameters)
        {
            this.memory = memory;
            this.video = video;
            this.audio = audio;
            this.input = input;
            this.parameters = parameters;

            if (timer != null)
                timer.Tick += Interrupt;

            RandomSeed = (int)(DateTime.Now.Ticks & 0x7FFFFFFF);
        }

        public IAudio Audio
        {
            get { return audio; }
        }

        public IDisplay Display
        {
            get { return display; }
        }

        public byte[] Dump()
        {
            return memory.Dump();
        }

        void Interrupt(object sender, EventArgs e)
        {
            InterruptTimer = true;
            InterruptTotal++;
        }

        public IMemory Memory
        {
            get { return memory; }
        }

        public int Random(int possibleValues)
        {
            return random.Next(possibleValues);
        }

        public int RandomSeed
        {
            get { return randomSeed; }
            set { randomSeed = value; random = new Random(randomSeed); }
        }

        public IVideo Video
        {
            get { return video; }
        }
    }
}
